package stringpackage

import (
	"fmt"
	"strconv"
	"strings"
)

func test1() {
	fmt.Println(strings.HasPrefix("this is string", "this"))
	fmt.Println(strings.HasPrefix("this is string", "1this"))

	fmt.Println(strings.HasSuffix("this is string", "ing"))
	fmt.Println(strings.HasSuffix("this is string", "iing"))
}

func test2() {
	totalString := "hello go, i love cpp"
	containString1 := "go"
	containString2 := "cpp"
	containString3 := "java"

	fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString1, totalString, strings.Contains(totalString, containString1))
	fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString2, totalString, strings.Contains(totalString, containString2))
	fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString3, totalString, strings.Contains(totalString, containString3))
}

func test3() {
	totalString := "hello go, i love cpp, i love cpp and go"
	containString1 := "go"
	containString2 := "cpp"
	containString3 := "java"

	fmt.Println("first index demo is:")
	fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString1, totalString, strings.Index(totalString, containString1))
	fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString2, totalString, strings.Index(totalString, containString2))
	fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString3, totalString, strings.Index(totalString, containString3))

	fmt.Println("last index demo is:")
	fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString1, totalString, strings.LastIndex(totalString, containString1))
	fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString2, totalString, strings.LastIndex(totalString, containString2))
	fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString3, totalString, strings.LastIndex(totalString, containString3))
}

func test4() {
	str1 := "hello hello hello hello hello hello"
	str2 := strings.Replace(str1, "hello", "no", -1)

	fmt.Println(str2)
}

func test5() {
	str1 := "hello world hello world hellhello world"
	fmt.Println(strings.Count(str1, "hello"))
	fmt.Println(strings.Count(str1, "world"))
}

func test6() {
	str := "hello go"
	fmt.Println(strings.Repeat(str, 10))
	fmt.Println(strings.Repeat(str, 2))
}

func test7() {
	str := "hello World This is TEST"

	fmt.Println(strings.ToLower(str))
	fmt.Println(strings.ToUpper(str))
}

func test8() {
	str1 := "11hello world111"
	str2 := "  hello go    "

	fmt.Println("去除空白")
	fmt.Println(strings.TrimSpace(str1))
	fmt.Println(strings.TrimSpace(str2))

	fmt.Println("去除左侧空白")
	fmt.Println(strings.TrimLeft(str2, " "))

	fmt.Println("去除左侧字符1")
	fmt.Println(strings.TrimLeft(str1, "1"))

	fmt.Println("去除右侧字符1")
	fmt.Println(strings.TrimRight(str1, "1"))

	fmt.Println("去除左右两侧1")
	fmt.Println(strings.Trim(str1, "1"))
}

func test9() {
	str1 := "hello1 hello2 hello3      hello4"

	fmt.Println("以一个空格为分隔符")
	s1 := strings.Split(str1, " ")

	for _, t := range s1 {
		fmt.Println(t)
	}

	fmt.Println("以一个或多个空格为分隔符")

	s2 := strings.Fields(str1)

	for _, t := range s2 {
		fmt.Println(t)
	}

	str3 := "hello11hello22hello321321312hello4"

	fmt.Println("以hello为分隔符")
	s3 := strings.Split(str3, "hello")

	for _, t := range s3 {
		fmt.Println(t)
	}
}

func test10() {
	// 定义一个字符串切片
	sl := []string{"apple", "banana", "cherry"}
	// 使用空格作为分隔符拼接切片中的字符串
	result := strings.Join(sl, " ")
	fmt.Println(result) // 输出 "apple banana cherry"

	// 使用逗号和空格作为分隔符拼接切片中的字符串
	result2 := strings.Join(sl, ", ")
	fmt.Println(result2) // 输出 "apple, banana, cherry"
}

func test11() {
	str1 := "666"
	number, _ := strconv.Atoi(str1)
	fmt.Println(number)
	fmt.Println(strconv.Itoa(number + 5))
}

func Test() {
	test11()
}
